package com.zenithmind.document.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.document.pojo.query.DocumentFavoriteQueryDTO;
import com.zenithmind.document.pojo.vo.DocumentFavoriteVO;
import com.zenithmind.document.service.DocumentFavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 文档收藏控制器
 * 遵循单一职责原则：专注于收藏相关的HTTP接口
 * 遵循依赖倒置原则：依赖抽象服务接口
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/document/favorite")
@RequiredArgsConstructor
@Validated
@Tag(name = "文档收藏管理", description = "文档收藏相关接口")
public class DocumentFavoriteController {

    private final DocumentFavoriteService documentFavoriteService;

    @PostMapping("/add")
    @Operation(summary = "收藏文档", description = "将文档添加到收藏夹")
    public Result<Boolean> favoriteDocument(
            @Parameter(description = "文档ID", required = true) @RequestParam @NotBlank String documentId,
            @Parameter(description = "收藏夹名称") @RequestParam(required = false, defaultValue = "默认收藏夹") String folderName) {
        
        String userId = UserContext.getUserId();
        Boolean result = documentFavoriteService.favoriteDocument(documentId, userId, folderName);
        
        return Result.success(result);
    }

    @DeleteMapping("/remove")
    @Operation(summary = "取消收藏文档", description = "从收藏夹中移除文档")
    public Result<Boolean> unfavoriteDocument(
            @Parameter(description = "文档ID", required = true) @RequestParam @NotBlank String documentId) {
        
        String userId = UserContext.getUserId();
        Boolean result = documentFavoriteService.unfavoriteDocument(documentId, userId);
        
        return Result.success(result);
    }

    @PostMapping("/batch/add")
    @Operation(summary = "批量收藏文档", description = "批量将文档添加到收藏夹")
    public Result<Integer> batchFavoriteDocuments(
            @Parameter(description = "文档ID列表", required = true) @RequestBody @NotEmpty List<String> documentIds,
            @Parameter(description = "收藏夹名称") @RequestParam(required = false, defaultValue = "默认收藏夹") String folderName) {
        
        String userId = UserContext.getUserId();
        Integer result = documentFavoriteService.batchFavoriteDocuments(documentIds, userId, folderName);
        
        return Result.success(result);
    }

    @DeleteMapping("/batch/remove")
    @Operation(summary = "批量取消收藏文档", description = "批量从收藏夹中移除文档")
    public Result<Integer> batchUnfavoriteDocuments(
            @Parameter(description = "文档ID列表", required = true) @RequestBody @NotEmpty List<String> documentIds) {
        
        String userId = UserContext.getUserId();
        Integer result = documentFavoriteService.batchUnfavoriteDocuments(documentIds, userId);
        
        return Result.success(result);
    }

    @GetMapping("/check")
    @Operation(summary = "检查文档是否已收藏", description = "检查指定文档是否已被当前用户收藏")
    public Result<Boolean> isFavorited(
            @Parameter(description = "文档ID", required = true) @RequestParam @NotBlank String documentId) {
        
        String userId = UserContext.getUserId();
        Boolean result = documentFavoriteService.isFavorited(documentId, userId);
        
        return Result.success(result);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询收藏", description = "分页查询当前用户的收藏文档")
    public Result<IPage<DocumentFavoriteVO>> getFavoritePage(@Valid DocumentFavoriteQueryDTO queryDTO) {
        
        // 设置当前用户ID
        queryDTO.setUserId(UserContext.getUserId());
        
        IPage<DocumentFavoriteVO> result = documentFavoriteService.getFavoritePage(queryDTO);
        
        return Result.success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "获取收藏列表", description = "获取当前用户的收藏文档列表")
    public Result<List<DocumentFavoriteVO>> getUserFavorites(
            @Parameter(description = "收藏夹名称") @RequestParam(required = false) String folderName,
            @Parameter(description = "限制数量") @RequestParam(required = false, defaultValue = "20") Integer limit) {
        
        String userId = UserContext.getUserId();
        List<DocumentFavoriteVO> result = documentFavoriteService.getUserFavorites(userId, folderName, limit);
        
        return Result.success(result);
    }

    @GetMapping("/folders")
    @Operation(summary = "获取收藏夹列表", description = "获取当前用户的收藏夹列表")
    public Result<List<String>> getUserFavoriteFolders() {
        
        String userId = UserContext.getUserId();
        List<String> result = documentFavoriteService.getUserFavoriteFolders(userId);
        
        return Result.success(result);
    }

    @PostMapping("/folder/create")
    @Operation(summary = "创建收藏夹", description = "创建新的收藏夹")
    public Result<Boolean> createFavoriteFolder(
            @Parameter(description = "收藏夹名称", required = true) @RequestParam @NotBlank String folderName,
            @Parameter(description = "收藏夹描述") @RequestParam(required = false) String description) {
        
        String userId = UserContext.getUserId();
        Boolean result = documentFavoriteService.createFavoriteFolder(userId, folderName, description);
        
        return Result.success(result);
    }

    @DeleteMapping("/folder/delete")
    @Operation(summary = "删除收藏夹", description = "删除指定的收藏夹")
    public Result<Boolean> deleteFavoriteFolder(
            @Parameter(description = "收藏夹名称", required = true) @RequestParam @NotBlank String folderName) {
        
        String userId = UserContext.getUserId();
        Boolean result = documentFavoriteService.deleteFavoriteFolder(userId, folderName);
        
        return Result.success(result);
    }

    @PutMapping("/folder/rename")
    @Operation(summary = "重命名收藏夹", description = "重命名指定的收藏夹")
    public Result<Boolean> renameFavoriteFolder(
            @Parameter(description = "旧收藏夹名称", required = true) @RequestParam @NotBlank String oldFolderName,
            @Parameter(description = "新收藏夹名称", required = true) @RequestParam @NotBlank String newFolderName) {
        
        String userId = UserContext.getUserId();
        Boolean result = documentFavoriteService.renameFavoriteFolder(userId, oldFolderName, newFolderName);
        
        return Result.success(result);
    }

    @PutMapping("/move")
    @Operation(summary = "移动收藏到收藏夹", description = "将收藏移动到指定的收藏夹")
    public Result<Boolean> moveFavoritesToFolder(
            @Parameter(description = "收藏ID列表", required = true) @RequestBody @NotEmpty List<String> favoriteIds,
            @Parameter(description = "目标收藏夹名称", required = true) @RequestParam @NotBlank String targetFolderName) {
        
        String userId = UserContext.getUserId();
        Boolean result = documentFavoriteService.moveFavoritesToFolder(favoriteIds, targetFolderName, userId);
        
        return Result.success(result);
    }

    @GetMapping("/search")
    @Operation(summary = "搜索收藏", description = "搜索当前用户的收藏文档")
    public Result<List<DocumentFavoriteVO>> searchUserFavorites(
            @Parameter(description = "搜索关键词", required = true) @RequestParam @NotBlank String keyword,
            @Parameter(description = "限制数量") @RequestParam(required = false, defaultValue = "20") Integer limit) {
        
        String userId = UserContext.getUserId();
        List<DocumentFavoriteVO> result = documentFavoriteService.searchUserFavorites(userId, keyword, limit);
        
        return Result.success(result);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取收藏统计", description = "获取当前用户的收藏统计信息")
    public Result<Object> getFavoriteStatistics() {
        
        String userId = UserContext.getUserId();
        Object result = documentFavoriteService.getFavoriteStatistics(userId);
        
        return Result.success(result);
    }

    @GetMapping("/hot")
    @Operation(summary = "获取热门收藏文档", description = "获取热门收藏文档列表")
    public Result<List<Object>> getHotFavoriteDocuments(
            @Parameter(description = "限制数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        
        List<Object> result = documentFavoriteService.getHotFavoriteDocuments(limit);
        
        return Result.success(result);
    }

    @GetMapping("/recent")
    @Operation(summary = "获取最近收藏", description = "获取当前用户最近的收藏文档")
    public Result<List<DocumentFavoriteVO>> getRecentFavorites(
            @Parameter(description = "限制数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        
        String userId = UserContext.getUserId();
        List<DocumentFavoriteVO> result = documentFavoriteService.getRecentFavorites(userId, limit);
        
        return Result.success(result);
    }

    @GetMapping("/document/{documentId}/users")
    @Operation(summary = "获取文档收藏用户", description = "获取收藏指定文档的用户列表")
    public Result<List<DocumentFavoriteVO>> getDocumentFavoriteUsers(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String documentId,
            @Parameter(description = "限制数量") @RequestParam(required = false, defaultValue = "20") Integer limit) {
        
        List<DocumentFavoriteVO> result = documentFavoriteService.getDocumentFavoriteUsers(documentId, limit);
        
        return Result.success(result);
    }

    @PostMapping("/sync/{documentId}")
    @Operation(summary = "同步收藏统计", description = "同步指定文档的收藏统计信息")
    public Result<Boolean> syncFavoriteStatistics(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String documentId) {
        
        Boolean result = documentFavoriteService.syncFavoriteStatistics(documentId);
        
        return Result.success(result);
    }

    @PostMapping("/cleanup")
    @Operation(summary = "清理无效收藏", description = "清理无效的收藏记录")
    public Result<Integer> cleanupInvalidFavorites() {
        
        Integer result = documentFavoriteService.cleanupInvalidFavorites();
        
        return Result.success(result);
    }
}
